package cn.ichiva.dl.tushare.tu.data;

import cn.ichiva.tushare.sdk.tushare.TuDataCacheAble;
import cn.ichiva.tushare.sdk.common.TuShareUtils;
import cn.ichiva.tushare.sdk.tushare.TuConfig;
import cn.ichiva.tushare.sdk.tushare.entity.TuRequest;
import cn.ichiva.tushare.sdk.tushare.entity.TuTable;

import java.util.*;
import java.util.stream.Collectors;

import static cn.ichiva.dl.tushare.tu.Config.tuConfig;

/**
 * 上证（默认）交易日历
 *
 * 获取各大交易所交易日历数据,默认提取的是上交所
 */
public class TradeCal extends TuDataCacheAble {

    public TradeCal(TuConfig client) {
        super(client);

        TuRequest request = new TuRequest();
        request.setApi("trade_cal");
        request.setDescribe("获取各大交易所交易日历数据,默认提取的是上交所");

        HashMap<String,String> paramsMap = new HashMap<>();
        paramsMap.put("start_date","20000101");
        paramsMap.put("end_date", TuShareUtils.lastTUDateString());
        request.setParams(paramsMap);

        LinkedHashMap<String,String> fieldMap = new LinkedHashMap<>();
        fieldMap.put("exchange","交易所 SSE上交所 SZSE深交所 ");
        fieldMap.put("cal_date","日历日期 ");
        fieldMap.put("is_open","是否交易 0休市 1交易 ");
        fieldMap.put("pretrade_date","上一个交易日 ");

        request.setFieldMap(fieldMap);
        setRequest(request);
    }

    public List<String> getTradingDay(String start_date,String end_date) {
        HashMap<String, String> params = new HashMap<>();
        params.put("start_date",start_date);
        params.put("end_date",end_date);
        getRequest().setParams(params);
        return Arrays.stream(super.get().get().getItems())
                .filter(e -> "1".equals(e[2]))
                .map(e -> e[1]).collect(Collectors.toList());
    }

    public List<String> getTradingDay() {
        return getTradingDay(null,null);
    }

    @Override
    public Optional<TuTable> get() {
        return super.get();
    }

    public Optional<TuTable> get(String start_date,String end_date) {
        HashMap<String, String> params = new HashMap<>();
        params.put("start_date",start_date);
        params.put("end_date", end_date);
        getRequest().setParams(params);
        return super.get();
    }

    public static void main(String[] args) {
        new TradeCal(tuConfig).get().ifPresent(System.out::println);
    }
}
